<?php

namespace app\admin\controller\renovation;

use app\admin\controller\Base;
use app\common\model\FileModel;
use app\common\model\renovation\CategoryModel;

class Material extends Base
{
    /**
     * 列表
     */
    public function list()
    {
        $params = request()->only(['page', 'limit', 'category_id', 'name', 'type']);

        if (isset($params['name']) && !empty($params['name'])) {
            $where[] = ['name', 'like', "%{$params['name']}%"];
        }
        if (isset($params['category_id']) && !empty($params['category_id'])) {
            $categoryIds = (new CategoryModel)->getAllSonIds($params['category_id']);
            $where[] = ['category_id', 'in', array_merge([$params['category_id']], $categoryIds)];
        }
        if (isset($params['type']) && !empty($params['type'])) {
            $where[] = ['type', '=', $params['type']];
        } else {
            $where[] = ['type', '=', FileModel::TYPE_IMG];
        }

        $sqlObj = (new FileModel)->where($where)
            ->order('id', 'DESC')
            ->field('id,name,category_id,path,size,create_time');

        $list = $sqlObj->paginate([
            'list_rows' => $params['limit'] ?? 10,
            'page' => $params['page'] ?? 1
        ]);

        $categoryData = (new CategoryModel)->field('id,name')->column('id,name', 'id');
        foreach ($list->items() as &$v) {
            $v['url'] = get_file_url($v['path']);
            $v['size'] = get_file_size($v['size']);
            $v['category_name'] = $categoryData[$v['category_id']]['name'] ?? '';
        }

        return response_success('', [
            'count' => $list->total(),
            'data' => $list->items()
        ]);
    }

    /**
     * 移动素材
     */
    public function move()
    {
        $params = request()->only(['ids', 'category_id']);
        $this->validate($params, [
            'ids' => 'require',
            'category_id' => 'require'
        ], [
            'ids.require' => '请选择移动文件',
            'category_id.require' => '请选移动至的分类'
        ]);

        $categoryInfo = (new CategoryModel)->where('id', $params['category_id'])->find();
        if (!$categoryInfo) {
            return response_error('分类信息不存在或已删除');
        }

        (new FileModel)->whereIn('id', explode(',', $params['ids']))->update([
            'category_id' => $params['category_id']
        ]);

        return response_success();
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $params = request()->only(['id', 'name', 'category_id']);
        $this->validate($params, [
            'id' => 'require',
            'name' => 'require'
        ], [
            'id.require' => '请选择素材',
            'name.require' => '请输入名称'
        ]);

        (new FileModel)->where('id', $params['id'])->update([
            'category_id' => $params['category_id'],
            'name' => $params['name']
        ]);

        return response_success();
    }

    /**
     * 删除
     */
    public function del()
    {
        $params = request()->only(['ids']);
        $this->validate($params, [
            'ids' => 'require'
        ], [
            'ids.require' => '请选择删除素材'
        ]);

        $fileModel = new FileModel;
        if (!$fileModel->delFile($params['ids'])) {
            return response_error($fileModel->getError());
        }

        return response_success();
    }

}